package com.xx.frame;

import com.intellij.execution.ExecutionException;
import com.intellij.execution.Executor;
import com.intellij.execution.RunnerAndConfigurationSettings;
import com.intellij.execution.configurations.LocatableConfigurationBase;
import com.intellij.execution.executors.DefaultRunExecutor;
import com.intellij.execution.impl.DefaultJavaProgramRunner;
import com.intellij.execution.runners.ExecutionEnvironment;
import com.intellij.execution.runners.ProgramRunner;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.ui.content.Content;
import org.jetbrains.idea.maven.execution.MavenRunConfigurationType;
import org.jetbrains.idea.maven.execution.MavenRunnerParameters;
import org.jetbrains.idea.maven.execution.RunnerBundle;
import org.jetbrains.idea.maven.project.MavenProject;
import org.jetbrains.idea.maven.project.actions.RunBuildAction;
import org.jetbrains.idea.maven.utils.MavenUtil;
import org.jetbrains.plugins.terminal.ShellTerminalWidget;
import org.jetbrains.plugins.terminal.TerminalView;

import javax.swing.*;
import java.util.List;
import java.util.Objects;

/**
 * command 模块
 */
public class CMDService {

    private MavenService mavenService = MavenService.getInstance();
    private NotifyService notifyService = NotifyService.getInstance();

    private static CMDService service = new CMDService();

    public static CMDService getInstance() {
        return service;
    }

    private CMDService() {

    }

    /**
     * 执行指定打包
     */
    public void openTerminal(Project project, JPanel infoPanel, String command) {

        String tabName = "Quick Maven Package";
        try {
            //关掉已经打开的相同的 tab
            ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow("Terminal");
            if (Objects.nonNull(toolWindow)) {
                int aa = toolWindow.getContentManager().getContentCount();
                if (aa > 0) {
                    for (int i = 0; i < aa; i++) {
                        Content content = toolWindow.getContentManager().getContent(i);
                        if (Objects.nonNull(content) && Objects.equals(tabName, content.getDisplayName())) {
                            TerminalView.getInstance(project).closeTab(content);
                        }
                    }
                }
            }

            // 创建tab，执行命令
            ShellTerminalWidget w = TerminalView.getInstance(project).createLocalShellWidget(project.getBasePath(), tabName);
            w.executeCommand(command);
        } catch (Exception e) {
            notifyService.panelNotify(infoPanel, e.getLocalizedMessage());
        }
    }

    /**
     * 执行单个maven项目打包
     */
    public void packageSingleProject(Project project, JPanel infoPanel, List<String> goalList) {
        RunBuildAction action;
        LocatableConfigurationBase locatableConfigurationBase;

        List<MavenProject> rootMavenProject = mavenService.getRootMavenProject(project);
        if (rootMavenProject.size() == 0) {
            notifyService.panelNotify(infoPanel, "非maven项目或者配置错误");
            return;
        }

        try {
            ProgramRunner runner = DefaultJavaProgramRunner.getInstance();
            Executor executor = DefaultRunExecutor.getRunExecutorInstance();
            MavenRunnerParameters params = new MavenRunnerParameters();
            params.setGoals(goalList);
            params.setWorkingDirPath(rootMavenProject.get(0).getDirectory());

            // 关键点
            // org.jetbrains.idea.maven.execution.MavenRunConfigurationType.createRunnerAndConfigurationSettings
            RunnerAndConfigurationSettings settings = MavenRunConfigurationType.createRunnerAndConfigurationSettings(null, null, params, project);

            ExecutionEnvironment environment = new ExecutionEnvironment(executor, runner, settings, project);
            runner.execute(environment);
        } catch (ExecutionException var11) {
            MavenUtil.showError(project, RunnerBundle.message("notification.title.failed.to.execute.maven.goal", new Object[0]), var11);
        }
    }

}
